#ifndef _TASK_H
#define _TASK_H


#define MAX_SUBTASK_ATTRIBUTE_BLOB_LEN 100

struct SuperSubTaskHandler;

//子任务数据
struct SubTask
{
public:
    int    getType() const;
protected:
    int    type;

    //子任务数据储存区域,不同子任务类型不同解释
    int    data[MAX_SUBTASK_ATTRIBUTE_BLOB_LEN];

    friend class SuperSubTaskHandler;
};

//子任务处理器(处理一种类型的子任务)
struct SuperSubTaskHandler
{
public:
    virtual    bool    isComplete(const SubTask&)    = 0;
    virtual    void    onSubmitTask()    = 0;
    virtual    void    updateData(SubTask&, int aEventID, void* aArg) = 0;

protected:
    template<typename T>
    const T&    conversion(const SubTask& aSubTask)
    {
        return *(const T*)aSubTask.data;
    }
    template<typename T>
    T&    conversion(SubTask& aSubTask)
    {
        return *(T*)aSubTask.data;
    }
};

#define MAX_SUBTASK_TYPE_NUM 0xFF

//子任务处理器管理器
class SuperSubTaskHandlerMgr
{
public:
    void    addSuperSubTaskHandler(int aSubTaskType, SuperSubTaskHandler* pHandler);
    bool    isComplete(const SubTask& aSubTask);
    void    updateData(SubTask& aSubTask, int aEventID, void* arg);

private:
    SuperSubTaskHandler*    m_superSubTaskHandlers[MAX_SUBTASK_TYPE_NUM];
};

extern SuperSubTaskHandlerMgr g_superSubTaskHandlerMgr;

//one task object have MAX_SUB_TASK_NUM subtask
#define MAX_SUB_TASK_NUM 10

//任务实例
struct Task
{
public:
    int        getType() const;
    bool    isComplete()    const;
    void    updateData(int aEventID, void* aArg);

protected:
    int    type;
    int    id;
    SubTask    m_subTasks[MAX_SUB_TASK_NUM];
};

//任务实例处理器(不同种类任务具有不同的处理器)
struct SuperTaskHandler
{
public:
    virtual    bool    isComplete(const Task&)    = 0;
    virtual    void    onSubmitTask()    = 0;
};

#define MAX_TASKTYPE_NUM    0xff

class SuperTaskHandlerMgr
{
public:
    void        addSuperTaskHandler(int aTaskType, SuperTaskHandler* pHandler);

private:
    SuperTaskHandler*    m_superTaskHandlers[MAX_TASKTYPE_NUM];
};

extern SuperTaskHandlerMgr g_superTaskHandlerMgr;

#endif
